home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / xmodem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  7.9 KB  |  388 lines

  1. /*
  2.  * Miscellaneous routines to support the xmodem file transfer protocols.
  3.  */
  4.  
  5. #define TMP_FILE    "trunXXXXXX"
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include "config.h"
  12. #include "misc.h"
  13. #include "param.h"
  14. #include "xmodem.h"
  15.  
  16. #ifdef BSD
  17. #include <setjmp.h>
  18. jmp_buf gl_buf, rl_buf;
  19. #endif /* BSD */
  20.  
  21. /*
  22.  * Calculate the CRC for the given buffer
  23.  */
  24.  
  25. unsigned short
  26. calc_crc(buf, len)
  27. unsigned char *buf;
  28. int len;
  29. {
  30.     register int i;
  31.     unsigned short crc;
  32.     static unsigned short crctab[256] = {
  33.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  34.     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  35.     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  36.     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  37.     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  38.     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  39.     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  40.     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  41.     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  42.     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  43.     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  44.     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  45.     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  46.     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  47.     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  48.     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  49.     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  50.     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  51.     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  52.     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  53.     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  54.     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  55.     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  56.     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  57.     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  58.     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  59.     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  60.     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  61.     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  62.     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  63.     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  64.     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0};
  65.  
  66.     crc = 0;
  67.     for (i=0; i<len; i++)
  68.         crc = (crc<<8) ^ crctab[(crc>>8) ^ *buf++];
  69.  
  70.     return(crc);
  71. }
  72.  
  73. /*
  74.  * Calculate the checksum for the given buffer.
  75.  */
  76.  
  77. unsigned char
  78. calc_sum(buf, len)
  79. unsigned char *buf;
  80. int len;
  81. {
  82.     unsigned char sum;
  83.  
  84.     sum = 0;
  85.     while (--len >= 0)
  86.         sum += *buf++;
  87.  
  88.     return(sum);
  89. }
  90.  
  91. /*
  92.  * Get a single character from the line with a specified time-out period
  93.  * in seconds.  If the function times-out, it returns a -1.
  94.  */
  95.  
  96. static int gl_flag;
  97.  
  98. int
  99. getc_line(sec)
  100. unsigned int sec;
  101. {
  102.     extern int fd;
  103.     static int gl_force();
  104.     char c;
  105.     unsigned int alarm();
  106.  
  107.     signal(SIGALRM, (SIG_TYPE(*) ()) gl_force);
  108.     gl_flag = 0;
  109.  
  110.     alarm(sec);
  111.  
  112. #ifdef BSD
  113.     if (setjmp(gl_buf))
  114.         return(-1);
  115. #endif /* BSD */
  116.  
  117.     if (read(fd, &c, 1) <= 0) {
  118.         alarm(0);
  119.         return(-1);
  120.     }
  121.     if (gl_flag)
  122.         return(-1);
  123.     alarm(0);
  124.     return(c & 0xff);
  125. }
  126.  
  127. /* ARGSUSED */
  128. static int
  129. gl_force(dummy)
  130. int dummy;
  131. {
  132. #ifdef BSD
  133.     longjmp(gl_buf, 1);
  134. #else /* BSD */
  135.     signal(SIGALRM, (SIG_TYPE(*) ()) gl_force);
  136.     gl_flag = 1;
  137. #endif /* BSD */
  138. }
  139.  
  140. /*
  141.  * Same as above, but reads a bunch of characters.  The return code is
  142.  * now just a success/fail indicator.
  143.  */
  144.  
  145. static int rl_flag;
  146.  
  147. int
  148. fread_line(buf, len, sec)
  149. unsigned char *buf;
  150. unsigned int len, sec;
  151. {
  152.     extern int fd;
  153.     int n;
  154.     static int rl_force();
  155.     unsigned int try, alarm();
  156.  
  157.     signal(SIGALRM, (SIG_TYPE(*) ()) rl_force);
  158.     rl_flag = 0;
  159.  
  160.     alarm(sec);
  161.     while (len) {
  162.                     /* read at most CLIST_SIZ chars */
  163.         try = (len > CLIST_SIZ) ? CLIST_SIZ : len;
  164. #ifdef BSD
  165.         if (setjmp(rl_buf))
  166.             return(-1);
  167. #endif /* BSD */
  168.         if ((n = read(fd, (char *) buf, try)) <= 0) {
  169.             alarm(0);
  170.             return(-1);
  171.         }
  172.         if (rl_flag)
  173.             return(-1);
  174.         len -= n;
  175.         buf = buf + n;
  176.     }
  177.     alarm(0);
  178.     return(0);
  179. }
  180.  
  181. /* ARGSUSED */
  182. static int
  183. rl_force(dummy)
  184. int dummy;
  185. {
  186. #ifdef BSD
  187.     longjmp(rl_buf, 1);
  188. #else /* BSD */
  189.     signal(SIGALRM, (SIG_TYPE(*) ()) rl_force);
  190.     rl_flag = 1;
  191. #endif /* BSD */
  192. }
  193.  
  194. /*
  195.  * Put a character on the TTY line.  This serves no useful purpose other
  196.  * than making the code look pretty.
  197.  */
  198.  
  199. int
  200. putc_line(c)
  201. unsigned char c;
  202. {
  203.     extern int fd;
  204.  
  205.     return(write(fd, (char *) &c, 1));
  206. }
  207.  
  208. /*
  209.  * Cancel the file transfer.  Send several ^X's to the remote, followed
  210.  * by an equal number of backspaces (in case they have already aborted and
  211.  * we're really at the command line).
  212.  */
  213.  
  214. void
  215. cancel_xfer(up)
  216. int up;
  217. {
  218.     extern char file_name[15];
  219.  
  220.     if (!up && !strcmp(param->abort, "DELETE"))
  221.         unlink(file_name);
  222.  
  223.     putc_line(CAN);
  224.     putc_line(CAN);
  225.     putc_line(CAN);
  226.     putc_line(BS);
  227.     putc_line(BS);
  228.     putc_line(BS);
  229.     return;
  230. }
  231.  
  232. /*
  233.  * Shorten a file to a predetermined length.  Used to remove the ^Z
  234.  * padding from the end of files.  (Heaven help us, if one day a binary
  235.  * file actually has ^Z's as part of the end of the file).
  236.  */
  237.  
  238. int
  239. fix_length(file, len)
  240. char *file;
  241. long len;
  242. {
  243.     FILE *fp, *tempfp, *my_fopen();
  244.     register int num;
  245.     static int uid_link(), uid_unlink();
  246.     char *mktemp(), tempfile[128], buf[BUFSIZ], *strcpy();
  247.     char *s, *strrchr(), *strcat();
  248.     struct stat stbuf;
  249.  
  250.     if (stat(file, &stbuf) < 0)
  251.         return(1);
  252.                     /* see if we have any work to do */
  253.     if (len >= stbuf.st_size)
  254.         return(0);
  255.  
  256.     if (!(fp = my_fopen(file, "r")))
  257.         return(1);
  258.  
  259.     /*
  260.      * The temporary file should be in the same directory as the
  261.      * file being received because otherwise we'd have no way of
  262.      * guaranteeing they would be in the same file system.  (Hard
  263.      * links across different file systems aren't allowed).
  264.      */
  265.     strcpy(tempfile, file);
  266.     if (s = strrchr(tempfile, '/'))
  267.         *++s = '\0';
  268.     else
  269.         strcpy(tempfile, "./");
  270.  
  271.     strcat(tempfile, TMP_FILE);
  272.     mktemp(tempfile);
  273.  
  274.     if (!(tempfp = my_fopen(tempfile, "w"))) {
  275.         fclose(fp);
  276.         return(1);
  277.     }
  278.  
  279.     while (len != 0L) {
  280.         num = (len > BUFSIZ) ? BUFSIZ : len;
  281.         fread(buf, sizeof(char), num, fp);
  282.         if (fwrite(buf, sizeof(char), num, tempfp) != num) {
  283.             fclose(fp);
  284.             fclose(tempfp);
  285.             return(1);
  286.         }
  287.         len = len - (unsigned int) num;
  288.     }
  289.  
  290.     fclose(fp);
  291.     fclose(tempfp);
  292.  
  293.     if (uid_unlink(file) < 0)
  294.         return(1);
  295.  
  296.     if (uid_link(tempfile, file) < 0)
  297.         return(1);
  298.  
  299.     if (uid_unlink(tempfile) < 0)
  300.         return(1);
  301.  
  302.     return(0);
  303. }
  304.  
  305. static int
  306. uid_link(path1, path2)
  307. char *path1, *path2;
  308. {
  309.     int ret, status, euid, egid;
  310.     void _exit();
  311.  
  312. #ifdef SETUGID
  313. #ifdef SETUID_BROKE
  314.     switch(fork()) {
  315.         case 0:
  316.             setuid(getuid());
  317.             setgid(getgid());
  318.             _exit(link(path1, path2));
  319.         case -1:
  320.             fprintf(stderr, "uid_link: Can't fork\n");
  321.             return(-1);
  322.         default:
  323.             if (wait(&status) == -1) {
  324.                 fprintf(stderr, "uid_link: wait failed\n");
  325.                 return(-1);
  326.             }
  327.             ret = status >> 8;
  328.     }
  329. #else /* SETUID_BROKE */
  330.     euid = geteuid();
  331.     egid = getegid();
  332.  
  333.     setuid(getuid());
  334.     setgid(getgid());
  335.  
  336.     ret = link(path1, path2);
  337.  
  338.     setuid(euid);
  339.     setgid(egid);
  340. #endif /* SETUID_BROKE */
  341.     return(ret);
  342. #else /* SETUGID */
  343.     return(link(path1, path2));
  344. #endif /* SETUGID */
  345. }
  346.  
  347. static int
  348. uid_unlink(path)
  349. char *path;
  350. {
  351.     int ret, status, euid, egid;
  352.     void _exit();
  353.  
  354. #ifdef SETUGID
  355. #ifdef SETUID_BROKE
  356.     switch(fork()) {
  357.         case 0:
  358.             setuid(getuid());
  359.             setgid(getgid());
  360.             _exit(unlink(path));
  361.         case -1:
  362.             fprintf(stderr, "uid_unlink: Can't fork\n");
  363.             return(-1);
  364.         default:
  365.             if (wait(&status) == -1) {
  366.                 fprintf(stderr, "uid_unlink: wait failed\n");
  367.                 return(-1);
  368.             }
  369.             ret = status >> 8;
  370.     }
  371. #else /* SETUID_BROKE */
  372.     euid = geteuid();
  373.     egid = getegid();
  374.  
  375.     setuid(getuid());
  376.     setgid(getgid());
  377.  
  378.     ret = unlink(path);
  379.  
  380.     setuid(euid);
  381.     setgid(egid);
  382. #endif /* SETUID_BROKE */
  383.     return(ret);
  384. #else /* SETUGID */
  385.     return(unlink(path));
  386. #endif /* SETUGID */
  387. }
  388.